package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.ActorVo;
import com.school.movie.business.web.model.vo.MovieActorVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.MovieActorServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/movieActor")
public class MovieActorController {

    @Autowired
    MovieActorServiceImp movieActorService;
    @Autowired
    SysLogServiceImp logService;
    @Value("${upload.absPath}")
    String absPath;

    @RequestMapping("/list")
    public String list(int movieId, PageBean pager, HttpSession session, ModelMap map) {

        if (movieId == 0) {
            if (session.getAttribute("movieId") != null) {
                movieId = (Integer) session.getAttribute("movieId");
            }
        } else {
            session.setAttribute("movieId", movieId);
        }
        getParams(movieId, pager, map);
        return "movieActor";
    }

    @PostMapping("/save")
    public String save(HttpServletRequest request, HttpSession session, MovieActorVo movieActor, ModelMap map) throws Exception {
        int movieId = (Integer) session.getAttribute("movieId");
        movieActor.setMovieId(movieId);
        if (movieActor.getMovieActorId() > 0) {
            movieActorService.update(movieActor);
            logService.add(BaseConstants.LOG_UPDATE, movieActor.toString());
        } else {
            movieActorService.add(movieActor);
            logService.add(BaseConstants.LOG_ADD, movieActor.toString());
        }
        map = getParams(movieId, new PageBean(), map);
        return "movieActor";
    }

    private ModelMap getParams(int movieId, PageBean pager, ModelMap map) {
        int cnt = movieActorService.count(movieId);
        pager.setTotalRows(cnt);
        List<MovieActorVo> movieList = movieActorService.list(movieId, pager);
        pager.setData(movieList);
        List<ActorVo> actorList = movieActorService.actorList();
        map.addAttribute("pager", pager);
        map.addAttribute("actorList", actorList);
        map.addAttribute("movieActor", new MovieActorVo());
        return map;
    }

    @RequestMapping("/add")
    public String add(ModelMap map) {
        List<ActorVo> actorList = movieActorService.actorList();
        map.addAttribute("actorList", actorList);
        map.addAttribute("movieActor", new MovieActorVo());
        return "movieActor";
    }

    @RequestMapping("/update")
    public String update(MovieActorVo movieActor, ModelMap map) {
        movieActor = movieActorService.getById(movieActor.getMovieActorId());
        List<ActorVo> actorList = movieActorService.actorList();
        map.addAttribute("actorList", actorList);
        map.addAttribute("movieActor", movieActor);
        return "movieActor";
    }

    @RequestMapping("/del")
    public String del(String ids, ModelMap map, HttpSession session) {
        int movieId = (Integer) session.getAttribute("movieId");
        String[] arr = ids.split(",");
        movieActorService.del(arr);
        getParams(movieId, new PageBean(), map);
        return "movieActor";
    }
}
